package com.naiterui.ehp.bs.patient.dao.mongo;

import cn.hutool.core.collection.ListUtil;
import com.naiterui.common.repository.page.Page;
import com.naiterui.ehp.bp.constants.MessageTypeEnum;
import com.naiterui.ehp.bp.constants.MessageVisualType;
import com.naiterui.ehp.bs.patient.prodocl.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 负责消息数据的持久化和消息数据的获取
 * <p>
 * File name : MessageDao.java
 * </P>
 * <p>
 * Author : zhouyanxin
 * </P>
 * <p>
 * Date : 2015年6月28日
 * </P>
 */
@Repository
public class ApMessageDao extends MongoBaseDao {
    /**
     * 消息记录的collectionName
     */
    private static final String COLLECTION_NAME = "m_message";

    @Autowired
    protected MongoTemplate mongoTemplate;

    /**
     * 获取指定用户间聊天记录的个数 MessageDao.queryCountByTime()<BR>
     * <p>
     * Author : zhouyanxin
     * </P>
     * <p>
     * Date : 2015年6月29日
     * </P>
     *
     * @param beginTime
     * @param fromId
     * @param toId
     * @return
     */
    public long queryCountByTime(Long beginTime, Long fromId, Long toId) {

        // 日期和用户查询条件
        Criteria c1 = Criteria.where("from.id").is(fromId).and("to.id").is(toId);
        Criteria c2 = Criteria.where("from.id").is(toId).and("to.id").is(fromId);
        Criteria c3 = new Criteria().orOperator(c1, c2);
        Criteria criteria = Criteria.where("sendTime").lt(beginTime).andOperator(c3);

        // 查询
        Query query = Query.query(criteria).with(Sort.by(new Sort.Order(Direction.DESC, "sendTime")));

        // 返回查询结果
        return super.queryCount(query, COLLECTION_NAME);
    }

    /**
     * 获取指定用户间的聊天记录 MessageDao.pageQueryByTime()<BR>
     * <p>
     * Author : zhouyanxin
     * </P>
     * <p>
     * Date : 2015年6月29日
     * </P>
     *
     * @param beginTime
     * @param fromId
     * @param toId
     * @param page
     * @param num
     * @param orderby
     * @return
     */
    public List<Message> pageQueryByTime(Long beginTime, Long fromId, Long toId, int page, int num, String orderby) {

        // 日期和用户查询条件
        Criteria c1 = Criteria.where("from.id").is(fromId).and("to.id").is(toId);
        Criteria c2 = Criteria.where("from.id").is(toId).and("to.id").is(fromId);
        Criteria c3 = new Criteria().orOperator(c1, c2);
        Criteria criteria = Criteria.where("sendTime").lt(beginTime).andOperator(c3);

        // 分页
        int skipNum = (page - 1) * num;

        // 查询
        Query query;
        if (Page.ASC.equals(orderby)) {
            query = Query.query(criteria).skip(skipNum).limit(num).with(Sort.by(new Sort.Order(Direction.ASC, "sendTime")));
        } else {
            query = Query.query(criteria).skip(skipNum).limit(num).with(Sort.by(new Sort.Order(Direction.DESC, "sendTime")));
        }

        // 返回查询结果
        return super.find(query, Message.class, COLLECTION_NAME);
    }

    public List<Message> queryFirstMessages(Long fromId, Long toId) {
        Criteria c1 = Criteria.where("from.id").is(fromId).and("to.id").is(toId);
        Criteria c2 = Criteria.where("from.id").is(toId).and("to.id").is(fromId);
        Criteria c3 = new Criteria().orOperator(c1, c2);
        // 查询
        Query query;
        query = Query.query(c3).with(Sort.by(new Sort.Order(Direction.DESC, "sendTime"))).limit(1);

        return super.find(query, Message.class, COLLECTION_NAME);
    }

    public List<com.naiterui.ehp.bs.patient.vo.im.Message> getPatientDisease(String sessionId) {
        Criteria c1 = Criteria.where("session.sessionId").is(sessionId).and("type").is(10004);
        Query query;
        query = Query.query(c1).with(Sort.by(new Sort.Order(Direction.DESC, "sendTime"))).limit(1);
        return super.find(query, com.naiterui.ehp.bs.patient.vo.im.Message.class, COLLECTION_NAME);
    }

    @Override
    protected MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }


    public List<com.naiterui.ehp.bs.patient.vo.consult.Message> getLastMessageByIdAndVisualType(Long fromId) {
        // 消息可见类型全部可见和患者可见
        Criteria c1 = Criteria.where("from.id").is(fromId).and("relation").is(0).and("visualType").in(ListUtil.toList(MessageVisualType.ALL.getType(), MessageVisualType.PATIENT.getType()));
        Criteria c2 = Criteria.where("to.id").is(fromId).and("relation").is(1).and("visualType").in(ListUtil.toList(MessageVisualType.ALL.getType(), MessageVisualType.PATIENT.getType()));
        Criteria c3 = Criteria.where("from.id").is(fromId).and("relation").is(0).and("type").is(MessageTypeEnum.REMIND_PATIENTS_FILL_MEDICAL_RECORD.getType());
        Criteria c4 = Criteria.where("to.id").is(fromId).and("relation").is(1).and("type").is(MessageTypeEnum.REMIND_PATIENTS_FILL_MEDICAL_RECORD.getType());
        Criteria c5 = new Criteria().orOperator(c1, c2, c3, c4);
        // 查询
        Query query = Query.query(c5).with(Sort.by(new Sort.Order(Direction.DESC, "sendTime")));
        return super.find(query, com.naiterui.ehp.bs.patient.vo.consult.Message.class, COLLECTION_NAME);
    }

}
